package com.hyq.controller;


import com.hyq.model.Teacher;
import com.hyq.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
public class TeacherController {
	
	@Autowired
	public TeacherService service;

	

//老师验证用户名是否重复
	@RequestMapping("validateNameTeacher")
	@ResponseBody
	public boolean validateNameTeacher(String name){
		return service.validateNameTeacher(name);

	}
	
	

//老师注册
	@RequestMapping("sRegisterTeacher")
	public String sRegisterTeacher(Teacher t){

		int val = service.sRegisterTeacher(t);
		if(val > 0){
			return "sLogin-teacher.jsp";
		}else{
			return "sRegister-teacher.jsp";
		}

	}



//老师登录
	@RequestMapping("sLoginTeacher")
	public String sLoginTeacher(Teacher t,HttpServletRequest request){
		Teacher newT = service.sLoginTeacher(t);
		if(newT != null){
			request.getSession().setAttribute("TeacherInfo", newT);
			return "index";
		}else{
			return "index";
		}
		
		
	}


//老师注销
	@RequestMapping("logOutTeacher")
	public String logOutTeacher(HttpServletRequest request){
		request.getSession().removeAttribute("TeacherInfo");
		return "index";
	}
	

//老师更改密码
	@RequestMapping("updatePasswordTeacher")
	@ResponseBody
	public String updatePasswordTeacher(HttpServletResponse response,String oldPassword,String newPassword,int tid){

		response.setContentType("text/json;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		int val1 = service.cOldPasswordTeacher(oldPassword, tid); 

		if(val1 > 0){
			int val2 = service.updatePasswordTeacher(newPassword, tid);

			if(val2 > 0){

				return "Modified success!";
			}else{

				return "Modification failed!";
			}
		}else{
			return "Original password error!";
		}
		
	}
	

//老师的赋文本编辑
	@RequestMapping("UeditorServlet")
	public String doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
 
 
		request.setCharacterEncoding("UTF-8"); 
		response.setCharacterEncoding("UTF-8"); 
		String content = request.getParameter("editorValue"); 
		if(content != null){ 
			request.setAttribute("content",content);
			return "sOpen.jsp";
		}else{ 
			response.getWriter().append("����Ϊ��!");//这里乱码了。。。。。
			return "sEdit.jsp";
		}
		
	}
	
//老师的修改头像， 依然也是没成功
//	@RequestMapping("tupdate")
//	public String tupdate(@RequestParam(value="img",required=false)CommonsMultipartFile myfile,Teacher te,HttpServletRequest request ){
//
//
//		try {
//			String oldName = myfile.getOriginalFilename();
//			String suffix = oldName.substring(oldName.lastIndexOf("."));
//			long time = System.currentTimeMillis();
//			String newName = time + "" + Math.floor(Math.random()*10000);
//			myfile.getFileItem().write(new File("D:/pic/" + newName + suffix));
//
//			te.setTimg("/kkk/" + newName + suffix);
//
//			int val = service.tupdate(te);
//			Teacher newTe = service.tsearchById(te.getTid());
//			request.getSession().setAttribute("TeacherInfo", newTe);
//
//			return "sPersonal-teacher.jsp";
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
//		return "sPersonal-teacher.jsp";
//
//	}
	
//修改老师的同户名
	@RequestMapping("updateTnameTeacher")
	@ResponseBody
	public String updateTnameTeacher(String tname,int tid){

		int val1 = service.updateTnameTeacher(tname, tid); 
		
		if(val1 > 0){
			
			return "Modified success!";
			
		}else{
			return "Original password error!";
		}
		
	}

	
//管理员操作搜索老师
	@RequestMapping("tSearch")
	@ResponseBody
	public  List<Teacher> tSearch(String keyword){
		
		List<Teacher> l = service.tSearch(keyword);
		
		return l;
		
	}
	

//管理员操作，批量删除老师账号，这个可以
	@RequestMapping("tBatchDelete")
	@ResponseBody
	public int tBatchDelete(String arr[]){
		int val=service.tBatchDelete(arr);
		if(val>0){
			return 1;
		}else{
			return 0;
		}
	}

	
//管理员操作修改老师信息，这个不行
	@RequestMapping("tUpdate")
	@ResponseBody
	public List<Teacher> tUpdate(int tid){

		List<Teacher> l = service.tUpdate(tid); 
		
		return l;
	}
	
	
//老师找回密码，这个也不太行
	@RequestMapping("tRefind")
	public  String tRefind(int tid,HttpServletRequest request){
		
		Teacher t = service.tRefind(tid);
		request.getSession().setAttribute("TeacherInfo", t);
		return "sPersonal-teacher.jsp";
		
	}
	
	

}
